package com.xbongbong.util;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.FieldPermissionEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ListGroupEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/14 16:31
 */
public class ApproveUtil {

    /**
     * 根据字段权限设置可见规则
     * @param explainMap
     * @param fieldPermissionMap
     */
    public static void setVisibleByFieldPermission(Map<String, FieldAttrEntity> explainMap, Map<String, FieldPermissionEntityExt> fieldPermissionMap) {
        Iterator<String> iterator = explainMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            FieldAttrEntity fieldAttrEntity = explainMap.get(key);
            FieldPermissionEntityExt fieldPermissionEntityExt = fieldPermissionMap.get(key);
            if (Objects.nonNull(fieldPermissionEntityExt) && Objects.nonNull(fieldAttrEntity)) {
                fieldAttrEntity.setVisible(fieldPermissionEntityExt.getVisible());
                if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
                    List<? extends FieldAttrEntity> subFieldList = fieldAttrEntity.getSubForm().getItems();
                    Iterator<? extends FieldAttrEntity> iterator1 = subFieldList.iterator();
                    while (iterator1.hasNext()) {
                        FieldAttrEntity subField = iterator1.next();
                        fieldPermissionEntityExt = fieldPermissionMap.get(subField.getParentAttr() + StringConstant.POINT + subField.getAttr());
                        if (Objects.nonNull(fieldPermissionEntityExt)) {
                            subField.setVisible(fieldPermissionEntityExt.getVisible());
                        }
                    }
                }
            }
        }
    }

    /**
     * 过滤审批中数据
     * @param businessType
     * @param subBusinessType
     * @param defaultGroup
     * @param listGroupId
     * @param list
     * @return void
     * @author 吴峰
     * @date 2021/12/16 18:48
     */
    public static void filterApprovalData(Integer businessType, Integer subBusinessType, Integer defaultGroup, Long listGroupId, List<ConditionsEntityExt> list) {
        if(Objects.equals(XbbRefTypeEnum.PREPAYMENT_BALANCE.getCode(), businessType) || Objects.equals(subBusinessType, XbbRefTypeEnum.WARNING_SEARCH.getCode())
                || Objects.equals(XbbRefTypeEnum.ORI_STOCK.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.PREPAY_BALANCE.getCode(),businessType)
                || Objects.equals(XbbRefTypeEnum.DISTRIBUTOR_ACCOUNT.getCode(), businessType) || Objects.equals(XbbRefTypeEnum.CUSTOMER_STATEMENT.getCode(), businessType)){
            return;
        }
        boolean isDefaultApprovalGroup = Objects.equals(defaultGroup, 1) && Objects.equals(listGroupId, Long.valueOf(ListGroupEnum.APPROVAL.getCode()));
        // 从公海池来的审批中
        boolean isPublicApprovalGroup = Objects.equals(defaultGroup, 2) && Objects.equals(listGroupId, Long.valueOf(ListGroupEnum.APPROVAL.getCode()));
        if (isPublicApprovalGroup) {
            ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt(FieldTypeEnum.FLOW_STATUS.getAlias(), "", FieldTypeEnum.FLOW_STATUS.getType(), ConditionEnum.IN.getSymbol(), Arrays.asList(FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType()));
            list.add(conditionsEntityExt);
        } else if (!isDefaultApprovalGroup) {
            ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt(FieldTypeEnum.FLOW_STATUS.getAlias(), "", FieldTypeEnum.FLOW_STATUS.getType(), ConditionEnum.IN.getSymbol(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType()));
            list.add(conditionsEntityExt);
        }
    }
}
